﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using JPW.OnlinePaperSubmissionSystem.Project.Dto;
using Kendo.DynamicLinq;
using System.Data.Entity;
using Abp.Notifications;

namespace JPW.OnlinePaperSubmissionSystem.Project
{

    // [AbpAuthorize(AppPermissions.Pages_Tenant_Announcement)]
    public class AnnouncementAppService : AbpZeroTemplateAppServiceBase, IAnnouncementAppService
    {
        private readonly IRepository<Announcement, long> _announcementRepository;
        private readonly INotificationPublisher _notificationPublisher;
        private readonly IUserNotificationManager _userNotificationManager;
        public AnnouncementAppService(IRepository<Announcement, long> announcementRepository, INotificationPublisher notificationPublisher, IUserNotificationManager userNotificationManager)
        {
            _announcementRepository = announcementRepository;
            _notificationPublisher = notificationPublisher;
            _userNotificationManager = userNotificationManager;
        }

        public DataSourceResult KendoGetAnnouncements(DataSourceRequest input)
        {
               var announcements = _announcementRepository
               .GetAll()
               .OrderBy(p => p.Id)
               .MapTo<List<AnnouncementListDto>>().AsQueryable()
               .ToDataSourceResult(input);

            return announcements;
        }
        public ListResultOutput<AnnouncementListDto> GetAnnouncements(GetAnnouncementInput input)
        {
            var announcements = _announcementRepository
            .GetAll()
            //.WhereIf(
            //    !input.Filter.IsNullOrEmpty(),
            //    p => p.Name.Contains(input.Filter) ||
            //            p.Code.Contains(input.Filter) 
            //||p.Performance==Convert.ToInt32(input.Filter)
            //)
            .OrderBy(p => p.Id)
            .ToList();

            return new ListResultOutput<AnnouncementListDto>(announcements.MapTo<List<AnnouncementListDto>>());
        }
        public async Task<ListResultOutput<AnnouncementListDto>> GetAnnouncementsAsync()
        {
            var query = await _announcementRepository.GetAll().OrderBy(p => p.Id).ToListAsync();

            return new ListResultOutput<AnnouncementListDto>(query.MapTo<List<AnnouncementListDto>>());

        }

        //  [AbpAuthorize(AppPermissions.Pages_Tenant_Announcement_Delete)]
        public async Task DeleteAnnouncement(IdInput input)
        {
            await _announcementRepository.DeleteAsync(input.Id);
        }

        public async Task CreateOrUpdateAnnouncement(CreateOrUpdateAnnouncementInput input)
        {
            if (input.Announcement.ID.HasValue)
            {
                await UpdateAnnouncementAsync(input);
            }
            else
            {
                await CreateAnnouncementAsync(input);
            }
        }
        //  [AbpAuthorize(AppPermissions.Pages_Tenant_Announcement_Create)]
        public async Task CreateAnnouncementAsync(CreateOrUpdateAnnouncementInput input)
        {
            var announcement = input.Announcement.MapTo<Announcement>();
            await _announcementRepository.InsertAsync(announcement);
        }
        //  [AbpAuthorize(AppPermissions.Pages_Tenant_Announcement_Update)]
        public async Task UpdateAnnouncementAsync(CreateOrUpdateAnnouncementInput input)
        {
            var announcement = input.Announcement.MapTo<Announcement>();
            await _announcementRepository.UpdateAsync(announcement);
        }
        //  [AbpAuthorize(AppPermissions.Pages_Tenant_Announcement_Update)]
        public async Task<GetAnnouncementForEditOutput> GetAnnouncementForEdit(NullableIdInput input)
        {
            Announcement announcement = null;
            if (input.Id.HasValue) //Editing existing role?
            {
                announcement = await _announcementRepository.FirstOrDefaultAsync(input.Id.Value);
            }
            return new GetAnnouncementForEditOutput
            {
                Announcement = announcement.MapTo<AnnouncementEditDto>(),
            };
        }

        public async Task SendAnnouncement(SendAnnouncementInput input)
        {
            long[] arry = new long[input.ids.Length];
            for (int i = 0; i < input.ids.Length; i++)
            {
                arry[i] = Convert.ToInt64(input.ids[i]);
            }
            await _notificationPublisher.PublishAsync(
                input.Title,
                new MessageNotificationData(input.Content),
                severity: NotificationSeverity.Info,
                userIds: arry
                );
        }
    }
}

